import eraqDlg from "@/utils/dialog";

export const strUtils = {
    // 字符串判空
    isEmpty: function (str) {
        return typeof str === "undefined" || !str || str === "null" || str === "";
    },
    checkEmpty: function (str) {
        if (typeof str === "undefined" || !str || str === "") {
            eraqDlg.warning("传入参数为空");
            return false;
        }
        return true;
    },
    getUrlParam: function (key) {
        let url = window.location + "";
        let keyIdx = url.indexOf(key);
        let hashIdx = url.indexOf("#");

        //URL中不包含参数关键字，直接返回空值
        if (keyIdx < 0) {
            return "";
        }

        //非hash模式下，直接通过search方法取得URL参数
        let val;
        if (hashIdx < 0) {
            val = new URLSearchParams(window.location.search).get(key);
            return strUtils.isEmpty(val) ? "" : val;
        }

        //hash模式下，判断参数位置：#前还是#后
        if (keyIdx > hashIdx) {
            url = window.location.hash + "";
        }
        val = keyIdx > hashIdx ? url.substring(url.indexOf("?") + 1) : url.substring(url.indexOf("?") + 1, hashIdx);

        let params = val.split("&");
        val = "";
        for (let i = 0, n = params.length; i < n; i++) {
            let kv = params[i].split("=");
            if (kv[0] !== key) {
                continue;
            }
            val = kv[1];
        }
        return val;
    }
}

export const ajaxUtils = {
    // 数据请求结果
    checkFail: function (code, msg) {
        if (code !== 200 || msg !== "") {
            eraqDlg.alert(msg !== "" ? msg : "数据请求错误，操作失败");
            return true;
        }
        return false;
    },
    isFail: function (code, msg) {
        return code !== 200 || msg !== "";
    },
    showErr: function (err) {
        eraqDlg.error(strUtils.isEmpty(err) ? "数据请求异常" : err);
    }
}

export const eraqConstants = {
    FILE: {
        exts: ["image/jpeg", "image/jpg", "image/png", "image/bmp", "image/gif"],
        extNames: "jpg、png、bmp、gif",
        maxSize: 20
    },

}
